package com.zhixun.zhlq.utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author eollse
 */
public class BsResult extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final String CODE_KEY = "code";

    public static final String MSG_KEY = "msg";

    public static final String DATA_KEY = "data";

    public BsResult() {
        put(CODE_KEY, 0);
        put(MSG_KEY, "操作成功");
    }

    public static BsResult error() {
        return error(1, "操作失败");
    }

    public static BsResult error(String msg) {
        return error(1, msg);
    }

    public static BsResult error(int code, String msg) {
        BsResult r = new BsResult();
        r.put(CODE_KEY, code);
        r.put(MSG_KEY, msg);
        return r;
    }

    public static BsResult error(int code, String msg, String key, Object value) {
        BsResult r = new BsResult();
        r.replace(CODE_KEY, code);
        r.replace(MSG_KEY, msg);
        r.put(key, value);
        return r;
    }

    public static BsResult ok(String msg) {
        BsResult r = new BsResult();
        r.put(MSG_KEY, msg);
        return r;
    }

    public static BsResult ok(Map<String, Object> map) {
        BsResult r = new BsResult();
        r.putAll(map);
        return r;
    }

    public static BsResult ok(int code, Object object) {
        BsResult r = new BsResult();
        r.put(CODE_KEY, code);
        r.put("data", object);
        return r;
    }

    public static BsResult ok() {
        return new BsResult();
    }

    public static BsResult ok(int code, String msg) {
        BsResult r = new BsResult();
        r.put(CODE_KEY, code);
        r.put(MSG_KEY, msg);
        return r;
    }

    public static BsResult okPutData(int code, String msg, Object data) {
        BsResult r = new BsResult();
        r.put(CODE_KEY, code);
        r.put(MSG_KEY, msg);
        if (data != null)
            r.put("data", data);
        return r;
    }

    public static BsResult okPutAll(int code, String msg, Map map) {
        BsResult r = new BsResult();
        r.put(CODE_KEY, code);
        r.put(MSG_KEY, msg);
        r.putAll(map);
        return r;
    }

    @Override
    public BsResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static BsResult success(Object data) {
        BsResult r = new BsResult();
        r.put(CODE_KEY, 200);
        r.put(MSG_KEY, "操作成功");
        r.put(DATA_KEY, data);
        return r;
    }

    public static BsResult error500() {
        BsResult r = new BsResult();
        r.put(CODE_KEY, 500);
        r.put(MSG_KEY, "系统异常");
        return r;
    }
}
